<?php

namespace app\home\controller;

use think\Controller;
use think\Request;

class Goods extends Base
{
    public function index1($id){
        $data = \app\common\model\Goods::where("cate_id",$id)->order("id desc")->paginate(10);
        $cate_name = \app\common\model\Category::where("pid",$id)->find();
        // dump($data);die;
        return view("goods/index",["data" => $data,"cate_name"=>$cate_name]);
    }
    public function index($id=0)
    {
        //接收参数
        $keywords = input('keywords');
        if(empty($keywords)){
            //获取指定分类下商品列表
            if(!preg_match('/^\d+$/', $id)){
                $this->error('参数错误');
            }
            //查询分类下的商品
            $list = \app\common\model\Goods::where('cate_id', $id)->order('id desc')->paginate(10);
            //查询分类名称
            $category_info = \app\common\model\Category::find($id);
            $cate_name = $category_info['cate_name'];
        }else{
            try{
                //从ES中搜索
                $list = \app\home\logic\GoodsLogic::search();
                $cate_name = $keywords;
            }catch (\Exception $e){
                $this->error('服务器异常');
            }
        }
        return view('index', ['data' => $list, 'cate_name' => $cate_name]);
    }
    public function edit($id){
        # 根据id查询一条数据 进行goods图片连表查询 和 goods规格 值查询
        $goods_data = \app\common\model\Goods::with("goods_image,goods_spec")->find($id);   
        # 判断是goods规格是否有数据
        if($goods_data["goods_spec"]){
            # 将goods表里面价格 修改为 规格表里面的正确价格
            $goods_data["goods_price"] = $goods_data["goods_spec"][0]["price"];
        }
        # 使用数组函数 array_column
        $spec_ids = array_column($goods_data["goods_spec"],"value_ids");
        $value_ids = array_unique(explode("_",implode("_", $spec_ids)));
        // dump($value_ids);die;
        $spec_data = \app\common\model\SpecValue::alias("t1")
        ->join("pyg_spec t2 ","t1.spec_id=t2.id","left")
        ->field('t1.*, t2.spec_name')
        ->where("t1.id","in",$value_ids)
        ->select();
        $spec = [];
        foreach ($spec_data as $key => $value) {
            // dump($value);die;
            $spec[$value['spec_id']] = [
                "id" => $value["spec_id"],
                
                "spec_name" => $value["spec_name"],
                "spec_value" => []
            ];
        }
        foreach ($spec_data as $key => $value) {
            $spec[$value["spec_id"]]["spec_value"][] = [
                "id" => $value["id"],
                "spec_value" => $value["spec_value"]
            ];
        }
        // dump((new \think\Collection($spec))->toArray());die;
        $value_ids_map = [];
        foreach ($goods_data["goods_spec"] as $key => $value) {
            $value_ids_map[$value["value_ids"]] = [
                "id" => $value["id"],
                "price" => $value["price"],
            ];
        }
        $value_ids_map = json_encode($value_ids_map);
        // dump((new \think\Collection($goods_data))->toArray());die;
        // dump($spec);die;
        return view("goods/edit",["goods_data" => $goods_data,"spec" => $spec,"value_ids_map" => $value_ids_map]);
    }
}
